﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AbingtonBooks.Pages.Profile.NewBook
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["Customer"] == null) {
                Response.Redirect("~/Pages/UnauthorizedAccess");
            }

            if(!Page.IsPostBack){
                using (dbAbingtonBooks context = new dbAbingtonBooks())
                {
                    //var courses = context.Courses.Select(x => new { key = x.CourseId, value = x.CourseNumber }).ToList();
                    //listClasses.DataSource = courses;
                    //listClasses.DataValueField = "key";
                    //listClasses.DataTextField = "value";
                    //listClasses.DataBind();
                    var subjects = context.Courses.Select(x => x.Subject).Distinct().ToList();
                    listSubjects.DataSource = subjects;
                    listSubjects.DataBind();
                    listSubjects.Items.Insert(0, "All");
                }

            }

        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            Customer thisCustomer = (Customer)Session["Customer"];
            using (dbAbingtonBooks context = new dbAbingtonBooks()) {
                Book myBook = new Book();
                myBook.Title = txtTitle.Text;
                myBook.Author = txtAuthor.Text;
                myBook.Price = Convert.ToDecimal(txtPrice.Text);
                myBook.ISBN = txtISBN.Text;
                myBook.CustomerID = thisCustomer.CustomerId;
                string imageName = fileImage.FileName.ToString();
                string guid = Guid.NewGuid().ToString();
                string imagePath = "~/ImageStorage/" + guid + imageName;
                fileImage.SaveAs(Server.MapPath(imagePath));
                 Image myImage = new Image();
                if (fileImage.PostedFile != null && fileImage.PostedFile.FileName != "")
                {

                    if (fileImage.PostedFile.ContentLength > 10485760) //10485760 bytes means 10MB
                    {

                        Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('File is too big')", true);

                    }

                    else
                    {

                        myImage.Src = imagePath;
                        context.Images.Add(myImage);
                        context.SaveChanges();
                        myBook.ImageId = myImage.ImageId;
                        myBook.Summary = txtSummary.Text;
                        int courseId = Convert.ToInt32(listClasses.SelectedValue);
                        Course selectedCourse = context.Courses.Where(x => x.CourseId == courseId).FirstOrDefault();
                        myBook.Courses.Add(selectedCourse);
                        context.Books.Add(myBook);
                        context.SaveChanges();
                        Response.Redirect("~/Pages/Profile/MyBooks");
                    }

                }

            }
        }

        protected void listSubjects_TextChanged(object sender, EventArgs e)
        {
           
            string selectedSubject = listSubjects.SelectedValue;
            using (dbAbingtonBooks context = new dbAbingtonBooks()) {
                var result = context.Courses.Where(x => x.Subject == selectedSubject).Select(x => new { key = x.CourseId, value = x.CourseNumber }).ToList();
                if (result != null)
                {
                    listClasses.Items.Clear();
                    listClasses.DataSource = result;
                    listClasses.DataValueField = "key";
                    listClasses.DataTextField = "value";
                    listClasses.DataBind();
                    listClasses.Items.Insert(0, new ListItem("", ""));
                }
                else {
                    listClasses.Items.Clear();
                    listClasses.Items.Insert(0, new ListItem("", ""));
              
                }
            }
        }

        
    }
}